/*============================================================================*/
/*                   Tortoise Team					                          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            MAL_ADC.h
* version:         1.0
* %created_by:      Andres Torres Garcia %
* %date_created:    Sunday May 20 13:41:01 2012 %
*=============================================================================*/
/* DESCRIPTION : Header file for uC Abstraction layer for ADC                 */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the prototypes functions uC Abstraction layer */
/*   for ADC module               											  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 25/05/2012 |                               | Andres Torres
	1.1		   29/05/2012 |								  | Andres Torres     */
/* Integration under Continuus CM                                             */
/*==========================================================================*/

#ifndef MAL_ADC_H                               /* To avoid double inclusion */
#define MAL_ADC_H

/* Includes */
/* -------- */
/* Register definitions for selected microcontroller */
#include "stdtypedef.h"

/*======================================================*/ 
/* close variable declaration sections                  */
/*======================================================*/ 

/* Exported functions prototypes and macros */
/* ---------------------------------------- */

/* Exported defines */

/**
 * \defgroup Alignment ADC alignment
 * @{
 */
#define LEFT	0
#define RIGHT	1
/**@}*/

/**
 * \defgroup Modes ADC modes
 * @{
 */
#define ONE_SHOOT	0
#define SCAN		1
/**@}*/

/*! \def _32_MHz                                                                                  
  ADC Conversion time 1.25 us and analog clock of 32 MHz                                                                                     
 */
#define _32_MHz		0x00008606

/**
 * \defgroup Precision ADC extended internal channels
 * @{
 */
#define ANS_0	32
#define ANS_1	33
#define ANS_2	34
#define ANS_3	35
#define ANS_4	36
#define ANS_5	37
#define ANS_6	38
#define ANS_7	39
#define ANS_8	40
#define ANS_9	41
#define ANS_10	42
#define ANS_11	43
#define ANS_12	44
#define ANS_13	45
#define ANS_14	46
#define ANS_15	47
/**@}*/

/**
 * \defgroup ADC_Watchdog useful macros
 * @{
 */
#define ENABLE_WDGH_INT		(ADC.WTIMR.B.MSKWDG0H=1)				// IE for high wdg
#define DISABLE_WDGH_INT	(ADC.WTIMR.B.MSKWDG0H=0)				// IE for high wdg
#define ENABLE_WDGL_INT		(ADC.WTIMR.B.MSKWDG0L=1)				// IE for low wdg
#define DISABLE_WDGL_INT	(ADC.WTIMR.B.MSKWDG0L=0)				// IE for low wdg
#define LOW_WDG_FLG			(ADC.WTISR.B.WDG0L)
#define CLEAR_LOW_WDG_FLG	(ADC.WTISR.B.WDG0L=1)
#define CLEAR_HIGH_WDG_FLG	(ADC.WTISR.B.WDG0H=1)
/**@}*/

/* Functions prototypes */

/*! Configure the pin as ADC channel. 
   \param ub_Port The pin.
   \sa Version 
  */
extern void ConfigPinAs_ADC_Channel(T_UBYTE ub_Port);

/*! Configure the type of ADC Mode.
   \param ub_mode The mode of the ADC. SCAN == 1 and ONE_SHOOT == 0.
   \sa Version 
  */
extern void Config_ADC_Mode(T_UBYTE ub_mode);

/*! Write the conversion data left/right-aligned.
   \param ub_justified Left 1 and right 0 alignment.
   \sa Version 
  */
extern void JustifiedADCread(T_UBYTE ub_justified);

/*! Set the amount of channels to be sampling
   \param uw_channels0 Enable bits of normal sampling for channel 0 to 31 (precision channels)
   \param uw_channels1 Enable bits of normal sampling for channel 32 to 63 (extended internal channels)
   \param uw_channels2 Enable bits of normal sampling for channel 64 to 95 (external channels)
   \sa Version 
  */
extern void SetAmountOfChannels(T_ULONG uw_channels0, T_ULONG uw_channels1, T_ULONG uw_channels2);

/*! Set the conversion time of the ADC channels
   \param ul_channels0 associated to internal precision channels (0 to 31)
   \param ul_channels1 associated to extended internal channels (from 32 to 63)
   \param ul_channels2 associated to external channels (from 64 to 95)
   \sa Version 
  */
extern void SetConvertionTime(T_ULONG ul_channels0, T_ULONG ul_channels1, T_ULONG ul_channels2);

/*! Start the convertion of the channels enabled
   \sa Version 
  */
extern void ADCStartConvertion(void);

/*!  Check if the ADC convertion is complete
   \param	ub_pin ADC Channel	
   \return True if the convertion is complete, otherwise false.
   \sa Version 
  */
extern bool	ADCConvertionComplete(T_UBYTE ub_pin);

/*! Read the value of the ADC Channel 
   \param ub_pin ADC Channel
   \return Value of the ADC Channel
   \sa Version 
  */
extern T_UWORD	ADCReadValue(T_UBYTE ub_pin);

/*! Init the ADC watchdog low threshold 
   \param u8WtdChannel ADC watchdog channel
   \param u32AdcChannel ADC channel
   \param u32LowTreshold Low threshold value
   \sa Version 
  */
extern void vfnInit_Adc_WatchdogL(T_UBYTE ub_WtdChannel, T_ULONG ul_AdcChannel, T_ULONG ul_LowTreshold);

/*! Read the value of the ADC Channel 
   \param u8WtdChannel ADC watchdog channel
   \param u32AdcChannel ADC channel
   \param u32HighTreshold High threshold value
   \sa Version 
  */
extern void vfnInit_Adc_WatchdogH(T_UBYTE ub_WtdChannel, T_ULONG ul_AdcChannel, T_ULONG ul_HighTreshold);
#endif